//
//  APIBaseManager.h
//  BaoZhangWang
//
//  Created by 叶越悦 on 15/10/14.
//  Copyright © 2015年 BaoliNetworkTechnology. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "URLResponse.h"
#import "APIManagerApiCallBackDelegate.h"
#import "APIManagerValidator.h"
#import "APIManagerInterceptor.h"
#import "APIManager.h"
#import "URLResponse.h"

NS_ASSUME_NONNULL_BEGIN

@class APIBaseManager;
@protocol APICacheProtocol;
@protocol AFMultipartFormData;
typedef void (^AFConstructingBlock)(id <AFMultipartFormData> formData);
typedef void (^APICompletionBlock)(BOOL success, __kindof APIBaseManager *_Nonnull manager);
typedef void (^APIProgressBlock)(NSProgress *progress);

typedef NS_ENUM(NSUInteger, APIManagerStatus) {
    
    APIManagerStatusDefault,          //没有产生过API请求，这个是manager的默认状态。
    APIManagerStatusLoading,          //API请求中，尚未返回数据
    APIManagerStatusSuccess,          //API请求成功且返回数据正确，此时manager的数据是可以直接拿来使用的。
    APIManagerStatusSuccessFromCache, //数据从缓存中来的
    //以下是错误状态
    APIManagerStatusNoContent,   //API请求成功但返回数据不正确。如果回调数据验证函数返回值不为nil，manager的状态就会是这个。
    APIManagerStatusParamsError, //参数错误，此时manager不会调用API，因为参数验证是在调用API之前做的。
    APIManagerStatusHttpError,   //网络错误
    APIManagerStatusNoNetwork,   //网络不通。在调用API之前会判断一下当前网络是否通畅，这个也是在调用API之前验证的。
    
};

/**
 让manager能够获取调用API所需要的数据
 */
@protocol APIManagerParamSourceDelegate <NSObject>

@required
- (_Nullable id)paramsForApi:(APIBaseManager *_Nonnull)manager;

@end

@interface APIBaseManager : NSObject

@property (nonatomic, weak, nullable) id <APIManagerApiCallBackDelegate> delegate;    /**< 回调代理 */
@property (nonatomic, weak, nullable) id <APIManagerParamSourceDelegate> paramSource; /**< 参数代理 */
@property (nonatomic, weak, nullable) id <APIManagerValidator> validator;             /**< 校验器 */
@property (nonatomic, weak, nullable) id <APIManagerInterceptor> interceptor;         /**< 拦截器 */

@property (nonatomic, readonly) BOOL isReachable;        /**< 是否有网络 */
@property (nonatomic, readonly) APIManagerStatus status; /**< APIManager状态 */
@property (nonatomic, readonly) NSInteger requestIdentifier; /**< 请求ID */
@property (nonatomic, copy) APICompletionBlock completionBlock;
@property (nonatomic, strong, nullable) id requestParam; /**< 请求的param */

@property (nonatomic, strong, readonly) URLResponse *response;
@property (nonatomic, strong, readonly, nullable) id responseObject; /**< 下载下来的数据 */
@property (nonatomic, strong, nullable) id reformResponseObject;     /**< 转换后的数据 */

@property (nonatomic) APIRequestPolicy requestPolicy; /**< 默认使用 kAPIRequestPolicyReturnCacheDataElseReloadRemoteData */
@property (nonatomic, strong) id <APICacheProtocol> cache;

@property (nonatomic, copy, nullable) AFConstructingBlock constructingBodyBlock; /**< 提交数据block */
@property (nonatomic, copy, nullable) APIProgressBlock uploadProgressBlock;
@property (nonatomic, copy, nullable) APIProgressBlock downloadProgressBlock;

@property (nonatomic, strong, readonly) NSError *error;

+ (instancetype)managerWithParamSource:(id <APIManagerParamSourceDelegate>)paramSource
                              delegate:(id <APIManagerApiCallBackDelegate>)delegate;
+ (instancetype)managerWithDelegate:(id <APIManagerApiCallBackDelegate>)delegate;

/**
 *  @author 叶越悦, 15-10-14 16:39:28
 *
 *  发起网络请求
 *
 *  @return 返回请求的ID
 */
- (NSInteger)loadData;
- (NSInteger)loadDataWithParam:(_Nullable id)params;
- (NSInteger)loadDataDidComplete:(_Nullable APICompletionBlock)complete;
- (NSInteger)loadDataWithParam:(_Nullable id)params complete:(APICompletionBlock _Nullable)complete;

//验证回调数据是否正确
- (void)cancelAllRequests;
- (void)cancelAllRequestsAndIgonerCallBack;
- (void)cancelRequestWithRequestID:(NSInteger)requestID;

@end

NS_ASSUME_NONNULL_END
